package cn.xiangyuanpin.service.impl;

import cn.xiangyuanpin.dao.BookDao;
import cn.xiangyuanpin.dao.impl.BookDaoImpl;
import cn.xiangyuanpin.pojo.Book;
import cn.xiangyuanpin.pojo.Page;
import cn.xiangyuanpin.service.BookService;


import java.util.List;

public class BookServiceImpl implements BookService {
    private BookDao bookDao=new BookDaoImpl();
    @Override
    public void addBook(Book book) {
        bookDao.addBook(book);
    }

    @Override
    public void deleteBookById(Integer id) {
bookDao.deleteBookById(id);
    }

    @Override
    public void updateBook(Book book) {
bookDao.updateBook(book);
    }

    @Override
    public Book queryBookById(Integer id) {
        return bookDao.queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() {
        return bookDao.queryBooks();
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page=new Page<Book>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

      int pageTotalCount=bookDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);

        Integer pageTotal=pageTotalCount/pageSize;

        if(pageTotalCount%pageSize>0){
            pageTotal=pageTotal+1;
        }
        page.setPageTotal(pageTotal);
int begin=(page.getPageNo()-1)*pageSize;
        List<Book> items=bookDao.queryForPageItems(begin,pageSize);
      page.setItems(items);




        return page;
    }


}
